#ifndef FAULTCACHEMODEL_H
#define FAULTCACHEMODEL_H


#include "imagerecord.h"

#include <QString>
#include <QAbstractListModel>
#include <QVector>
#include <QMutex>
#include "AutoPropertyHelper.h"

class FaultCacheModel : public QAbstractListModel
{
    Q_OBJECT
    Q_PROPERTY(int currentCacheNum  READ currentCacheNum NOTIFY currentCacheNumChange)
    Q_PROPERTY(int currentCacheSeclectId READ currentCacheSeclectId WRITE setcurrentCacheSeclectId NOTIFY currentCacheSeclectIdChange)

public:
    enum DataRoles {
        IdRole = Qt::UserRole + 1,
        DetectDetailRole,
        DetectTimeRole,
    };

    struct FaultImageCacheItem {
        int id;
        int time;
        QString detail;
        cv::Mat aiImage;
        cv::Mat resultImage;
        NgCard ngcard;
        QVector<cv::Mat> imageList;
        QVector<QPoint> imagePointList;
        bool isTrain;
    };
    Q_INVOKABLE void displayEmChart(int id); //给出弹窗图片
    Q_INVOKABLE void ClearCacheNum();
    Q_INVOKABLE void displayImageRecordById(int id);
    Q_INVOKABLE void eyeaADDTrain(int id);

    int  currentCacheSeclectId() const {return currentCacheSeclectId_;};
    void setcurrentCacheSeclectId(int id);
    static FaultCacheModel& instance();


    int currentCacheNum() const { return currentCacheNum_; };
    void displaycurrentCacheNum(int currentCacheNum);
    void clearIndex();
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    QHash<int, QByteArray> roleNames() const override;
    FaultImageCacheItem getImageEmListBy(int index);

    void removeRowByIndex(int index);
    QVector<FaultImageCacheItem> &getCacheList() {return m_imgCardCache;};

public slots:
    void addDataItem(ImageRecord imgCard);

signals:
    void finishAddDetectCache(int id , ImageRecord imgCard);
    void clearDetectCache();
    void currentCacheNumChange();
    void currentCacheSeclectIdChange();
    void drawistanceLargeSmall(int id ,float top,float bottom,
                               int topmax,int topmin,int bottommax,int bottommin);
    void okNumChange();
    void characterDetectionimgChanged();

private:
    QVector<FaultImageCacheItem> m_imgCardCache;
    FaultCacheModel(QObject *parent = nullptr);
    FaultCacheModel(const FaultCacheModel&) = delete;
    FaultCacheModel& operator=(const FaultCacheModel&) = delete;
    int currentCacheNum_;
    int cacheNum_ ;
    int index;
    int currentCacheSeclectId_;
    int okNum_;
    mutable QMutex mutex_; // 用于保护多线程访问
};

#endif // FAULTCACHEMODEL_H
